/*
 * Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于pps-cloud系统.
 */
package com.pitop.material.ctrl;


import com.alibaba.fastjson.JSON;
import com.pitop.account.entity.Account;
import com.pitop.base.ctrl.BaseCtrl;
import com.pitop.core.entity.BeanRet;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.tools.StringTools;
import com.pitop.core.tools.redis.RedisUtils;
import com.pitop.material.VO.MaterialVO;
import com.pitop.material.entity.Material;
import com.pitop.material.entity.MaterialState;
import com.pitop.material.service.MaterialSVImpl;
import com.pitop.material.service.MaterialSupplierSVImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 供应料材 控制器
 *
 * @author berton
 */
@RestController
@RequestMapping("/material")
@Slf4j
@Api(value = "供应料材控制器", description = "供应料材控制器")
public class MaterialCtrl  extends BaseCtrl{

    @Resource
    protected RedisUtils redisUtils;
    @Autowired
    private MaterialSVImpl materialSVImpl;
    @Autowired
    private MaterialSupplierSVImpl materialSupplierSV;

    /**
     * 查询Material一个详情信息
     *
     * @param id          id
     * @param code        编码
     * @param suppierCode 供应商编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Material一个详情信息", notes = "查询Material一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "query"),
            @ApiImplicitParam(name = "suppierCode", value = "供应商编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public BeanRet load(Long id, String code, String suppierCode) {
        if (id == null) {
            return null;
        }
        if (code == null) {
            return null;
        }
        if (suppierCode == null) {
            return null;
        }
        MaterialVO materialVO = materialSVImpl.load(id, code);
        log.info(JSON.toJSONString(materialVO));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, materialVO);
    }


    /**
     * 根据条件id查询Material一个详情信息
     *
     * @param id id
     * @return BeanRet
     */
    @ApiOperation(value = "查询Material一个详情信息", notes = "查询Material一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/load/id/{id}")
    @ResponseBody
    public BeanRet loadById(@PathVariable Long id) {
        if (id == null || id == 0) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        MaterialVO materialVO = materialSVImpl.loadById(id);
        log.info(JSON.toJSONString(materialVO));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, materialVO);
    }

    /**
     * 根据条件code查询Material一个详情信息
     *
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Material一个详情信息", notes = "查询Material一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/code/{code}")
    @ResponseBody
    public BeanRet loadByCode(@PathVariable String code) {
        if (StringUtils.isEmpty(code)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        MaterialVO materialVO = materialSVImpl.loadByCode(code);
        log.info(JSON.toJSONString(materialVO));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, materialVO);
    }


    /**
     * 查询Material信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询Material信息集合", notes = "查询Material信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "materialCategoryCode", value = "材料类型编码", paramType = "query"),
            @ApiImplicitParam(name = "sortColumns", value = "排序：code DESC, code ASE", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态", paramType = "query"),
            @ApiImplicitParam(name = "createTimeBegin", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "createTimeEnd", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTimeBegin", value = "更新时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTimeEnd", value = "更新时间", paramType = "query")
    })
    @GetMapping(value = "/list")
    @ResponseBody
    public BeanRet list(@ApiIgnore Material material, @ApiIgnore Page<MaterialVO> page) {
        if (page == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        List<MaterialVO> materialVOs = materialSVImpl.list(material, page.getCurPage(), page.getPageSize());
        page.setVoList(materialVOs);
        int count = materialSVImpl.count(material);
        page.setTotalRow(count);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "", page);
    }


    /**
     * 根据组件编码查询物料列表
     *
     * @return 分页对象
     */
    @ApiOperation(value = "根据组件编码查询物料列表", notes = "根据组件编码查询物料列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "assemblyCode", value = "组件编码", paramType = "query", required = true),
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query"),
    })
    @GetMapping(value = "/listForAssembly")
    @ResponseBody
    public BeanRet listForAssembly(String assemblyCode, String name, @ApiIgnore Page<MaterialVO> page) {
        if (page == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringTools.isEmpty(assemblyCode)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        List<MaterialVO> materialVOs = materialSVImpl.listForAssembly(assemblyCode, MaterialState.Enable.name(), name, page.getCurPage(), page.getPageSize());
        page.setVoList(materialVOs);
        int count = materialSVImpl.countForAssembly(assemblyCode, MaterialState.Enable.name(), name);
        page.setTotalRow(count);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "", page);
    }


    /**
     * 根据bom编码查询物料列表
     *
     * @return 分页对象
     */
    @ApiOperation(value = "根据bom编码查询物料列表", notes = "根据bom编码查询物料列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "bomCode", value = "bom编码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "物料名称", paramType = "query"),
    })
    @GetMapping(value = "/listForBom")
    @ResponseBody
    public BeanRet listForBom(String bomCode, String name, @ApiIgnore Page<MaterialVO> page) {
        if (page == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }

        if (StringTools.isEmpty(bomCode)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        List<MaterialVO> materialVOs = materialSVImpl.listForBom(bomCode, MaterialState.Enable.name(), name, page.getCurPage(), page.getPageSize());
        page.setVoList(materialVOs);
        int count = materialSVImpl.countForBom(bomCode, MaterialState.Enable.name(), name);
        page.setTotalRow(count);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "", page);
    }


    /**
     * 查询Material信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询Material信息集合", notes = "查询Material信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "suppierCode", value = "供应商编码", paramType = "query")
    })
    @GetMapping(value = "/list/by")
    @ResponseBody
    public BeanRet listByPk(Long id, String code, String suppierCode, @ApiIgnore Page<MaterialVO> page) {
        if (page == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        List<MaterialVO> materials = materialSVImpl.list(id, code, page.getCurPage(), page.getPageSize());
        int total = materialSVImpl.count(id, code);
        page.setTotalRow(total);
        page.setVoList(materials);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "", page);
    }

    /**
     * 创建供应料材
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建供应料材", notes = "创建供应料材")
    @PostMapping("/build")
    @ResponseBody
    public BeanRet build(@RequestBody MaterialVO materialVO, HttpServletRequest request) {
        if (StringUtils.isEmpty(materialVO.getNum())) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param, "编号");
        }
        if (StringUtils.isEmpty(materialVO.getName())) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param, "名称");
        }
        if (StringUtils.isEmpty(materialVO.getSpecifications())) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param, "规格");
        }
        if (StringUtils.isEmpty(materialVO.getBrandCode())) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param, "品牌编码");
        }
        if (StringUtils.isEmpty(materialVO.getMaterialCategoryCode())) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param, "材料分类编码");
        }

        //保存材料
        Account account = this.loadAccount(request);
       materialSVImpl.save(materialVO, account.getAccount(), account.getCode());

        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success);
    }


    /**
     * 修改供应料材
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改供应料材", notes = "修改供应料材")
    @PostMapping("/modify")
    @ResponseBody
    public BeanRet modify(@RequestBody MaterialVO materialVO) {
        if (StringUtils.isEmpty(materialVO.getCode())) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param, "编码");
        }
        if (StringUtils.isEmpty(materialVO.getNum())) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param, "编号");
        }
        if (StringUtils.isEmpty(materialVO.getName())) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param, "名称");
        }
        if (StringUtils.isEmpty(materialVO.getSpecifications())) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param, "规格");
        }
        if (StringUtils.isEmpty(materialVO.getBrandCode())) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param, "品牌编码");
        }
        if (StringUtils.isEmpty(materialVO.getMaterialCategoryCode())) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param, "材料分类编码");
        }
       /* if (StringUtils.isEmpty(material.getImageUrl())) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param, "图片");
        }*/
        /*if (StringUtils.isEmpty(material.getSummary())) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param, "描述");
        }*/
        materialSVImpl.modify(materialVO);

        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success);
    }

    /**
     * 修改供应料材状态
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改供应料材状态", notes = "修改供应料材状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态(1009)：启用 Enable,禁用 Disable", paramType = "query")
    })
    @PutMapping("/updateState")
    @ResponseBody
    public BeanRet updateState(String code, MaterialState state) {
        if (StringUtils.isEmpty(code)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (state == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        materialSVImpl.updateSate(code, state);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success);
    }


}
